<form class="form-horizontal" ENCTYPE="multipart/form-data" id="registerHere" method='post' action='?page=addsektoral&action=add'>
  <fieldset>
	<legend>TAMBAH SEKTORAL</legend>
	<?php
	
	$o = "";
	
	/* alert yang muncul setelah proses */
	if (isset($_GET['success']) && ($_GET['success'] == "1")) {
	
		$o .= '<div class="alert alert-success">
				Proses tambah sektoral berhasil
				</div>';
				
	} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses tambah sektoral gagal
			   </div>';
	} elseif (isset($_GET['upload']) && ($_GET['upload'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses upload gambar gagal
			   </div>';
	} elseif (isset($_GET['upload']) && ($_GET['upload'] == "1")) {
		
		$o .= '<div class="alert alert-error">
				Proses upload gambar gagal
			   </div>';
	}
		
	echo $o;
	
	?>
	
	<div class="control-group">
	  <label class="control-label" for="input01">Nama Sektoral</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="nama" name="nama" rel="popover" data-content="Masukkan nama sektoral." data-original-title="Nama Sektoral">
	  </div>
	</div>

	<div class="control-group">
		<label class="control-label" for="input01">Icon</label>
		<div class="controls">
			<input class="input-file" name="image" id="fileInput" type="file"  rel="popover" data-content="Masukan File Icon Berdasarkan Sektoral." data-original-title="Ikon" >
		</div>
	</div>

	<div class="control-group">
		<label class="control-label" for="input01"></label>
		  <div class="controls">
		   <button type="submit" class="btn btn-success" >Tambah Sektoral</button>
		  </div>
	</div>
  </fieldset>
</form>

<a href="?page=sektoral">kembali</a>

<?php
if ($_GET['action'] == "add")  {
	
	require ('../plugins/SimpleImage.php');

	$dim  = array('W'=>32, 'H'=>37);
	$uploaddir 	= "../file/icon/$picName";
	$picName 	= basename($_FILES['image']['name']);
	$nama_sektoral	= htmlentities(mysql_real_escape_string($_POST['nama']));
	
	if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		
		if((copy($_FILES['image']['tmp_name'], $uploaddir.$picName))) {
			
			$imgType = IMAGETYPE_JPEG;
			$imgDms = getimagesize($_FILES['image']['tmp_name']);
			if ($imgDms[0] > $dim['W']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToWidth($dim['W']);
				$image->save($uploaddir.$picName, $imgType);
			} elseif ($imgDms[1] > $dim['H']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToHeight($dim['H']);
				$image->save($uploaddir.$picName, $imgType);
			}
		} else {
				echo '
					<script language="javascript">
					document.location="?page=addsektoral&upload=0";
					</script>';
		}	
		
		$db = new Sektoral();
		$table = "sektoral";
		$sektoral = array('nama_sektoral'=>$_POST['nama'],'icon'=>$picName);
		$addsektoral = $db->tambah($table, $sektoral);
				
		if ($addsektoral) {
			echo '
				<script language="javascript">
				document.location="?page=addsektoral&success=1";
				</script>';
			
		} else {
			echo '
				<script language="javascript">
				document.location="?page=addsektoral&success=0";
				</script>';
		}
	} else {
		$db = new Sektoral();
		$table = "sektoral";
		$sektoral = array('nama_sektoral'=>$_POST['nama']);
		$addsektoral = $db->tambah($table, $sektoral);
				
		if ($addsektoral) {
			echo '
				<script language="javascript">
				document.location="?page=addsektoral&success=1";
				</script>';
			
		} else {
			echo '
				<script language="javascript">
				document.location="?page=addsektoral&success=0";
				</script>';
		}
	}	
}
?>




